package boot.demo.controller;

import boot.demo.event.SimpleEventListener;
import com.google.common.util.concurrent.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/async")
public class ListenableFutureController {
    private final static Logger LOGGER = LoggerFactory.getLogger(ListenableFutureController.class);
    @PostMapping("/try")
    public void doAsync() {
        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
        ListenableFuture explosion = service.submit(new Callable<String>() {
            @Override
            public String call() throws InterruptedException {
                Thread.sleep(2000);
                LOGGER.info("Thread is running ");
                return "call success";
            }
        });
        Futures.addCallback(explosion, new FutureCallback() {
            @Override
            public void onSuccess(Object result) {
                LOGGER.info("Thread run successed;result is  {} ", result);
            }

            @Override
            public void onFailure(Throwable t) {
                LOGGER.info("Thread run failed {} ", t);
            }
        });

    }
}
